<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
*@Name:  Sections Controller
*
*@Author:  Ervin R. Musngi
*
*/
class School_year extends MX_Controller {
    
    public function __construct()
	{
		parent::__construct();
		
		$this->record_per_page = $this->system_library->settings['record_per_page'];
		
		$this->load->library('users/auth');
		$this->load->library('form_validation');
        $this->load->language('school_year');
        $this->template
				->set_breadcrumb('Home','dashboard')
		   		->set_partial('header', 'partials/header')
				->set_partial('breadcrumb','partials/breadcrumb')
                ->set_partial('footer', 'partials/footer')
				->set_partial('navigation','partials/admin/menu')
                ->set_layout('default')
				->set_theme('default')
				->append_metadata($this->asset->css('reset.css'))
				->append_metadata($this->asset->css('general.css'))
				->append_metadata($this->asset->css('admin/default.css'))
				->append_metadata($this->asset->css('notifcation.slider.css'))
				->append_metadata($this->asset->css('admin/jquery.accordion.css'))
				->append_metadata($this->asset->js('jquery/jquery-1.6.2.min.js'))
				->append_metadata($this->asset->js('jquery.freeow.js'))
				->append_metadata($this->asset->js('notificationBox.jquery.js'))
				->append_metadata($this->asset->js('jquery_accordion_init.js'));
        
    }
    
    public function index()
    {
        $this->listing();
    }
	
	public function year($id_year = 0)
	{
		
		$school_year = new School_year_m();
		
		$data['school_year'] = $school_year->get_school_year_id($id_year);
		
		$this->template
					->append_metadata($this->asset->css('facebox.css'))
					->append_metadata($this->asset->js('jquery/facebox.js'))
					->set_breadcrumb('School Year',site_url('grading/school_year'))
					->set_breadcrumb($data['school_year']->year,'')
					->build('school_year/list_section',$data);
	}
	
	public function list_students( $id = 0 , $year = 0 )
	{
				
		if ( $id != 0 && $id != '' && $year != 0 && $year != '')
		{
			$std = $this->input->post('students',array());
			
			$student = new Student_m();
			
			if ( $student->list_students( $id, $std ) )
			{
				
				$this->session->set_flashdata('msg_title',lang('school_year_success_title'));
				$this->session->set_flashdata('msg',lang('school_year_success_update'));
				
				redirect('grading/school_year/year/'.$year);
			}
		}
		else
		{
			redirect('grading/school_year');
		}
		
	}
	
	public function lock( $id = 0 )
	{
		
		if ( isset($id) && $id != 0 )
		{
			
			$school_year = new School_year_m();
			
			$result = $school_year->lock_school_year( $id );
			
			if ( $result === false )
			{
				$data = array(
					'msg_title'	=> lang('school_year_error'),
					'msg'		=> lang('school_year_no_section')
				);
				
				$this->session->set_flashdata($data);
				
				
			}
			
			if ( is_array($result) )
			{
				$data = array(
					'msg_title'	=> lang('school_year_error'),
					'msg'		=> lang('school_year_no_student').implode(',',$result)
				);
				
				$this->session->set_flashdata($data);
			}
			elseif ( $result === true )
			{
				$data = array(
					'msg_title'	=> lang('school_year_success'),
					'msg'		=> lang('school_year_lock_success')
				);
				
				$this->session->set_flashdata($data);
			}
			
			redirect('grading/school_year');
			
			
			
		}
		else
		{
			redirect('grading/school_year/');
		}
		
	}
	
	public function create()
	{
		$school_year = new School_year_m();
		
		if ( $school_year->create_new_school_year() )
		{
			$data = array(
				'msg_title'	=> lang('school_year_title_success'),
				'msg'		=> lang('school_year_success')
			);
			
			$this->session->set_flashdata($data);
		}

		redirect('grading/school_year');

	}
	
	//public function student_listing($sy, $level, $section)
	//{
	//	echo $sy . '/' . $level . '/' . $section;
	//}
	
	public function student_listings($id = 0 , $year = 0)
	{
		if ( $id != 0 && $id != '' && $year != 0 && $year != '')
		{
			
			$student = new Student_m();
			
			$school_year = new School_year_m();
			
			$school_year->where('year',$year)->get();
			
			$data['section_list_id']	= $id;
			$data['year']				= $year;
			$data['is_lock']			= $school_year->is_lock;
			$data['students']			= $student->get_all($id,$school_year->id);
			$data['current_students']	= $student->get_current_student($id);
			
			if ( isAjax() )
			{
				$this->load->view('school_year/student_listings',$data);
			}
			else
			{
				$section = new Section_listing_m();
			
				$sect = $section->where('id',$id)->get();
				
				$this->template
					->set_breadcrumb('School Year','grading/school_year/')
					->set_breadcrumb($year,'grading/school_year/year/'.$year)
					->set_breadcrumb($sect->section_name)
					->build('school_year/student_listings',$data);
			}
		}
		else
		{
			echo json_encode(array('r'=>false));
		}
	}
	
	public function subject_listings( $section_id, $shool_level_id = 0 , $year = 0 )
	{
		if ( $section_id != 0 && $section_id != '' && $shool_level_id != 0 && $shool_level_id != '' && $year != 0 && $year != '')
		{
			
			$subjects = new Subject_listing_m();
			
			$school_year = new School_year_m();
			
			$school_year->where('year',$year)->get();
			
			$data['section_list_id']	= $section_id;
			$data['school_level_id']	= $shool_level_id;
			$data['year']				= $year;
			$data['subjects']			= $subjects->get_all( $section_id , $shool_level_id ,$school_year->id);
			
			if ( isAjax() )
			{
				$this->load->view('school_year/subject_listings',$data);
			}
			else
			{
				$section = new Section_listing_m();
			
				$sect = $section->where('id',$section_id)->get();
				
				$this->template
					->set_breadcrumb('School Year','grading/school_year/')
					->set_breadcrumb($year,'grading/school_year/year/'.$year)
					->set_breadcrumb($sect->section_name)
					->set_breadcrumb('Subjects')
					->build('school_year/subject_listings',$data);
			}
		}
		else
		{
			echo json_encode(array('r'=>false));
		}
	}
	
	public function listing($page=0)
	{
		if ($page < 0  || strlen($page) > 13)
		{
			redirect('grading/school_year/listing/');
		}
		else
		{
			$limit = $this->record_per_page;
			
			$student = new Student_m();
			
			$school_years  = new School_year_m();
			
			$school_year = $school_years->get_school_year(($limit + $page),$page);
			
			$total_rows = $school_year->count();
			
			if($total_rows < $page)
			{
				redirect('grading/subjects/listing/');
			}
			else
			{
				$this->load->library('pagination');
				
				$data['school_year']	= $school_year;
				
				//setup pagination
				$config['base_url']		= site_url('grading/students/listing/');
				$config['per_page']		= $limit;
				$config['uri_segment'] 	= 4;
				$config['total_rows']	= $total_rows;
				$config['anchor_class'] = 'class="btn"';
				$config['cur_tag_open'] = ' <div class="btn btn-disabled">';
				$config['cur_tag_close'] = '</div>';
				$this->pagination->initialize($config);
			
				$data['page'] 			= $this->pagination->create_links();
				
				$this->template
							->set_breadcrumb('School Year','')
							->build('school_year/list',$data);
			}
		}
	}
}